package com.sunseagear.wind.modules.sys.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.common.utils.ObjectUtils;
import com.sunseagear.common.utils.StringUtils;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.modules.sys.dto.GenerateButtonDto;
import com.sunseagear.wind.modules.sys.entity.Menu;
import com.sunseagear.wind.modules.sys.entity.vo.MenuVo;
import com.sunseagear.wind.modules.sys.service.IMenuService;
import com.sunseagear.wind.common.utils.UserUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sys/menu")
@Log(title = "菜单管理")
@RequiredArgsConstructor
public class MenuController extends BaseBeanController {
    private final IMenuService menuService;

    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     */
    @GetMapping(value = "list")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("sys:menu:list")
    public R<List<Menu>> list(String keyword) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        //加入条件
        queryWrapper.lambda().orderByAsc(Menu::getSort)
                .like(ObjectUtils.isNotEmpty(keyword), Menu::getName, keyword);
        List<Menu> list = menuService.selectTreeList(queryWrapper);
        return R.success(list);
    }

    /**
     * 添加菜单
     * @param entity 菜单实体
     * @return 操作结果
     */
    @PostMapping("add")
    @Log(logType = LogType.INSERT)
    @SaCheckPermission("sys:menu:add")
    public R<Menu> add(@Validated @RequestBody Menu entity) {
        menuService.save(entity);
        return R.success("添加成功", entity);
    }

    /**
     * 更新菜单
     * @param entity 菜单实体
     * @return 操作结果
     */
    @PutMapping("update")
    @Log(logType = LogType.UPDATE)
    @SaCheckPermission("sys:menu:update")
    public R<Void> update(@Validated @RequestBody Menu entity) {
        menuService.saveOrUpdate(entity);
        return R.success("更新成功");
    }

    /**
     * 获得菜单详情
     * @param id 菜单id
     * @return 详情结果
     */
    @GetMapping("detail/{id}")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("sys:menu:detail")
    public R<Menu> detail(@PathVariable("id") Long id) {
        Menu organization = menuService.getById(id);
        return R.success(organization);
    }

    /**
     * 删除菜单
     * @param idList 菜单id
     * @return 操作结果
     */
    @DeleteMapping("delete")
    @Log(logType = LogType.DELETE)
    @SaCheckPermission("sys:menu:delete")
    public R<Void> delete(@RequestBody List<Long> idList) {
        menuService.removeBatchByIds(idList);
        return R.success("删除成功");
    }

    /**
     * 获得菜单列表
     * @return 菜单列表
     */
    @GetMapping(value = "getMenus")
    @SaCheckLogin
    public R<List<MenuVo>> getMenus() {
        List<MenuVo> menuList = menuService.getCurrentUserMenuVos();
        return R.success(menuList);
    }

    /**
     * 获得权限列表
     * @return 菜单列表
     */
    @GetMapping(value = "getPermissions")
    @SaCheckLogin
    public R<List<String>> getPermissions() {
        //加入条件
        Long uid = UserUtils.getUser().getId();
        List<Menu> permissionValueList = menuService.findPermissionByUserId(uid);
        List<String> menuIdList = new ArrayList<>();
        for (Menu menu : permissionValueList) {
            menuIdList.add(menu.getPermission());
        }
        return R.success(menuIdList);
    }

    /**
     * 更新菜单排序
     * @param id 菜单id
     * @param sort 排序值
     * @return 操作结果
     */
    @GetMapping("{id}/changeSort")
    @SaCheckPermission("sys:menu:update")
    public R<Void> changeSort(@PathVariable("id") Long id,
                              @RequestParam("sort") Integer sort) {
        menuService.changeSort(id, sort);
        return R.success("排序成功");
    }

    /**
     * 生成按钮
     * @param id 菜单id
     * @param dto 参数
     * @return 操作结果
     */
    @PostMapping("{id}/generate/button")
    @Log(logType = LogType.OTHER, title = "生成按钮")
    @SaCheckPermission("sys:menu:generate:button")
    public R<Void> generateButton(@PathVariable("id") Long id,
                                  @Validated @RequestBody GenerateButtonDto dto) {
        menuService.generateButton(id, dto.getParentPermission(), dto.getPermissions().split(","), dto.getPermissionTitles().split(","), dto.getAdditional());
        return R.success("生成成功");
    }
}
